package cn.s1995.sloth.bms.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.s1995.sloth.bizs.annotation.RedisLock;
import cn.s1995.sloth.bizs.constant.BizCst;
import cn.s1995.sloth.bizs.constant.RedisKeyCst;
import cn.s1995.sloth.bizs.entity.Order;
import cn.s1995.sloth.bizs.entity.OrderItem;
import cn.s1995.sloth.bizs.service.OrderService;
import cn.s1995.sloth.bizs.service.ProdService;
import cn.s1995.sloth.bizs.service.SkuService;
import cn.s1995.sloth.bizs.service.common.CacheService;
import cn.s1995.sloth.quartz.constants.QuartzEnum;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * OrderJob
 *
 * @author Lex
 * @description 订单定时任务
 * @date 2024/01/03 09:58
 */
@Slf4j
@Component("orderJob")
public class OrderJob {

    @Resource
    private OrderService orderService;
    @Resource
    private ProdService productService;
    @Resource
    private SkuService skuService;
    @Resource
    private CacheService cacheService;

    /**
     * 定时任务-取消超时未支付订单
     * @return
     */
    @SneakyThrows
    @RedisLock(name = RedisKeyCst.ORDER_CANCEL, expire = 30)
    public String cancelOrder() {
        log.info("<===============定时任务-取消超时未支付订单===============>");
        LocalDateTime now = LocalDateTime.now();
        // 获取n分钟之前未支付的订单
        long orderAutoCancelTime = cacheService.getOrderAutoCancelTime();
        LocalDateTime lessThanUpdateTime = LocalDateTimeUtil.offset(now, -orderAutoCancelTime, ChronoUnit.MINUTES);
        List<Order> orders = orderService.getOrderAndOrderItems(BizCst.Order.State.unpay, lessThanUpdateTime);
        if (CollUtil.isNotEmpty(orders)) {
            orderService.cancelOrders(orders, Boolean.TRUE);
            for (Order order : orders) {
                List<OrderItem> orderItems = order.getOrderItems();
                for (OrderItem orderItem : orderItems) {
                    productService.removeProductCacheByProdId(orderItem.getProdId());
                    skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId());
                }
            }
        }

        log.info("<===============定时任务-取消超时未支付订单,处理完成完成({})===============>", orders.size());

        return QuartzEnum.JOB_LOG_STATUS_SUCCESS.getDescription();
    }

    /**
     * 定时任务-系统自动确认收货订单
     */
    @SneakyThrows
    @RedisLock(name = RedisKeyCst.ORDER_CONFIRM, expire = 30)
    public String confirmOrder() {
        log.info("<===============定时任务-系统自动确认收货订单===============>");
        LocalDateTime now = LocalDateTime.now();
        // 获取n天之前未支付的订单
        long orderAutoConfirmTime = cacheService.getOrderAutoConfirmTime();
        LocalDateTime lessThanUpdateTime = LocalDateTimeUtil.offset(now, -orderAutoConfirmTime, ChronoUnit.DAYS);
        List<Order> orders = orderService.getOrderAndOrderItems(BizCst.Order.State.consignment, lessThanUpdateTime);
        if (CollUtil.isNotEmpty(orders)) {
            orderService.confirmReceiptOrder(orders);
            for (Order order : orders) {
                List<OrderItem> orderItems = order.getOrderItems();
                for (OrderItem orderItem : orderItems) {
                    productService.removeProductCacheByProdId(orderItem.getProdId());
                    skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId());
                }
            }
        }

        log.info("<===============定时任务-系统自动确认收货订单,处理完成完成({})===============>", orders.size());
        return QuartzEnum.JOB_LOG_STATUS_SUCCESS.getDescription();
    }

}
